package com.example.demo.topic;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <pre>
 *  描述:
 * </pre>
 *
 * @author godelgnis(wujiaer)
 * @version 1.0.0
 * @date 2022/1/14 15:55
 */
public class TopicTest {
    public static void main(String[] args) {
        String topic = "/sys/+/data/post/+/+";
        String msg = "/sys/132/data/post/cmd123/json";
        List<String> list = new ArrayList<String>() {{
            add("/sys/132/data/post/cmd123/json");
            add("/sys/132/data/post/cmd123/json/123");
            add("/sys/132/data/post/");
            add("/sys/132/data/cmd123/json");
        }};

        String topicPattern = "/sys/(\\w)+?/data/post/(\\w)+?/(\\w)+?";

        topicPattern = topic.replaceAll("\\+", "(\\\\w)+?");
        System.out.println(topicPattern);
        Pattern pattern = Pattern.compile(topicPattern);
        for (String s : list) {
            Matcher matcher = pattern.matcher(s);
            boolean matches = matcher.matches();
            System.out.println(matches);
        }

    }
}
